import request from '@/config/axios'
import type { GroupOrgData } from './types'

export const getOrgGroupListApi = (params: any): Promise<IResponse> => {
    const queryParams = params.queryParams ? params.queryParams : {}
    if (params.queryParams) {
        delete params.queryParams
    }
    Object.assign(params, queryParams)
    params.pageNum = params.page_index
    params.pageSize = params.page_size
    delete params.page_index
    delete params.page_size
    return request.get({url: '/group/org/page', params})
}


export const saveOrUpdateOrgGroup = (data: Partial<GroupOrgData>, isUpdate = false): Promise<IResponse> => {
    if (isUpdate) {
        return request.put({url: '/group/org/', data})
    } else {
        return request.post({url: '/group/org/', data})
    }
}

export const getOrgGroupDetailApi = (id: number): Promise<IResponse<GroupOrgData>> => {
    return request.get({url: `/group/org/${id}`})
}

export const delOrgGroupApi = (data: Partial<GroupOrgData>[]): Promise<IResponse> => {
    if (data.length > 0) {
        const ids = data.map((d) => d.id);
        return request.delete({ url: `/group/org/delete/${ids.join(',')}` })
    } else {
        const id = data[0].id
        return request.delete({ url: `/group/org/${id}` })
    }
}